package com.my;

import javax.servlet.*;
import java.io.IOException;

/**
 * Created by YJH on 2017/7/12 14:40.
 */
public class UserNameFilter implements Filter {

    private FilterConfig filterConfig;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig = filterConfig;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        String initUser = filterConfig.getInitParameter("username");
        String username = servletRequest.getParameter("username");

        if (!initUser.equals(username)) {
            servletRequest.setAttribute("message","用户名不正确!");
            servletRequest.getRequestDispatcher("/login.jsp").forward(servletRequest, servletResponse);
            return;
        }

        filterChain.doFilter(servletRequest, servletResponse);
    }

    @Override
    public void destroy() {

    }
}
